//table title
var rm_columnData = [
    {"Width": "25%", "ColumnName": "角色ID"},
    {"Width": "25%", "ColumnName": "角色Code"},
    {"Width": "25%", "ColumnName": "角色名称"},
    {"Width": "25%", "ColumnName": "操作"}
];

//table 操作html
var rm_operateHtml = "<div class='form-inline'><button type='button' class='btn btn-primary innerButtons'" +
    " value='delete'>删除 </button> " +
    "<button type='button' class='btn btn-primary innerButtons' value='modify'>修改</button>" +
    " <button type='button' class='btn btn-primary innerButtons' data-toggle='modal' value='authorize'>分配</button>" +
    "</div>";

//授权 table title
var rp_columnData = [
    {
        "Width": "10%",
        "ColumnName": "<input type='checkbox' id='title-role-table-checkbox' name='title-table-checkbox'/>"
    },
    {"Width": "30%", "ColumnName": "权限ID"},
    {"Width": "30%", "ColumnName": "权限Code"},
    {"Width": "30%", "ColumnName": "权限名称"}
];

//角色管理分页函数
var gotoPage = function (index) {
    var roleCode = $("#roleCodeSearch").val();
    var postData = {"pageSize": pageSize, "roleCode": roleCode, "pageNumber": index};
    var url = webPath + "/crm/getRoleByPage.do";
    var options = {"passArguments": index};
    postAjax(url, postData, gotoPageSuccess, options);
};

//分页成功的回调函数
var gotoPageSuccess = function (result, arguement) {
    var columnList = ["roleId", "roleCode", "roleName"];
    cleanTableAndPage("roleManageTable");
    var jsonData = $.parseJSON(result);
    //画table
    $("#roleManageTable").MakeTable(false, rm_columnData, jsonData.list, tableClass, columnList, rm_operateHtml);
    //画分页
    $("#roleManageTable").after(comPagination(arguement, pageSize, jsonData.totalNumber, "gotoPage", "roleManageP"));
    //table操作栏 按钮处理 授权 删除 修改
    $("#roleManageTable button").click(function () {
        var that = $(this);
        var buttonType = that.val();
        if (buttonType === "authorize") {
            roleAuthorization(that);
        } else if (buttonType === "delete") {
            deleteRole(that);
        } else if (buttonType === "modify") {
            modifyRole(that);
        } else {
            alert("sometion wrong");
        }
    });
}


//增加
$("#addRoleBtn").click(function () {
    $("#roleAddModal").modal('show');
});

//角色增加成功的回调函数
var roleSaveSuccess = function (result) {
    $("#roleManageModal").modal('hide');
    gotoPage(getCurrentPage("roleManageTable"));
}

//查询
$('#searchRoleCode').click(function () {
    gotoPage(1);
});

//用户权限授权提交
$("#roleSaveBtn").click(function () {
    var selectRow = $("#rolePermissionTable tbody").find("input:checked");
    var arrayList = [];
    $.each(selectRow, function (i) {
        arrayList.push($(selectRow[i]).closest("tr").find("td div")[1].innerHTML);
    });
    var postData = {"priviledgeId": arrayList, "roleId": $("#roleIdDisabled").text()};
    var url = webPath + "/crm/savePriviledgeByRole.do";
    var options = {"successMessage": "授权成功！"};
    postAjax(url, postData, roleSaveSuccess, options);
});

//授权角色
var roleAuthorization = function (that) {
    var url = webPath + "/crm/getPriviledgeByRole.do";
    var roleId = getRoleId(that);
    var postData = {"roleId": roleId};
    postAjax(url, postData, roleAuthorizationSuccess);
}

var roleAuthorizationSuccess = function (result) {
    var columnList = ["priviledgeId", "priviledgeCode", "priviledgeName"];
    var jsonData = $.parseJSON(result);
    //清除旧数据
    cleanTableAndPage("rolePermissionTable");
    $("#roleNameDisabled").text(jsonData.object.roleName);
    $("#roleCodeDisabled").text(jsonData.object.roleCode);
    $("#roleIdDisabled").text(jsonData.object.roleId);
    $("#rolePermissionTable").MakeTable(true, rp_columnData, jsonData.list, tableClass, columnList);

    var tdList = $("#rolePermissionTable tbody td:nth-child(2)");
    var selectPriviledgeList = jsonData.object.priviledgeRoles;
    if (tdList && tdList.length > 0) {
        $.each(selectPriviledgeList, function (i) {
            tableCheckBoxChecked(tdList, selectPriviledgeList[i].priviledgeId + "");
        });
    }
    $("#title-role-table-checkbox").click(function () {
        var that = this;
        checkBoxSelectAll(that);
    });
    $("#roleManageModal").modal('show');
}

//增加角色
var roleAddSave = function () {
    var url = webPath + "/crm/saveRoleById.do";
    var postData = $("#addRoleForm").serialize();
    var options = {"successMessage": "角色添加成功！"}
    postAjax(url, postData, roleAddSaveSuccess, options);
};

var roleAddSaveSuccess = function (result) {
    $("#roleAddModal").modal('hide');
    gotoPage(getCurrentPage("roleManageTable"));
}

//删除角色
var deleteRole = function (that) {
    var roleId = getRoleId(that);
    var roleName = that.closest("tr").find("td div")[2].innerHTML;
    var url = webPath + "/crm/deleteRoleById.do";
    showDialogueBox({
        'id': 'confirmMsg',
        'type': 'confirm',
        'title': '删除提示',
        'message': '你确定要删除"' + roleName + '"角色吗？',
        'buttons': {
            'Yes': {
                'class': 'btn-primary',
                'action': function () {
                    var options = {"passArguments": that, "successMessage": "删除成功！"};
                    postAjax(url, {"roleId": roleId}, deleteRoleSuccess, options);
                }
            },
            'no': {
                'class': '',
                'action': function () {
                }
            }
        }
    });
};

var deleteRoleSuccess = function (result, that) {
    gotoPage(getCurrentPage(that.closest("table").attr("id")));
}

//修改角色
var modifyRole = function (that) {
    var tds = that.closest("tr").find("td div");
    var roleID = tds[0].innerHTML;
    var roleCode = tds[1].innerHTML;
    var roleName = tds[2].innerHTML;
    $("#roleID").val(roleID);
    $("#roleCode").val(roleCode);
    $("#roleNewName").val(roleName);
    $("#roleModifyModal").modal('show');
};

var roleModifySave = function () {
    var postData = $("#modifyRoleForm").serialize();
    var url = webPath + "/crm/updateRoleById.do";
    var options = {"successMessage": "修改成功！"}
    postAjax(url, postData, roleModifySaveSuccess, options);
}

var roleModifySaveSuccess = function () {
    $("#roleModifyModal").modal('hide');
    gotoPage(getCurrentPage("roleManageTable"));
}

var getRoleId = function (that) {
    return that.closest("tr").find("td div")[0].innerHTML;
}